#ifndef _jobmanager_h__
#define _jobmanager_h__
#include "system/lfqueue.h"
#include "gtl/list.h"
#include "system/threadevent.h"

// uncomment this line to make all jobs run on main thread for debugging purposes
//#define NO_MULTITHREADING

namespace gecko
{

	class Job;
	class JobThread;

	// manager for jobs and job threads
	class JobManager
	{
	public:
		// construct
		JobManager(unsigned int numJobThreads);

		// add a new job
		void AddJobToQueue(Job * job);

		// get a job from the queue
		Job * GetJob();

		// process all jobs
		void ProcessJobs();

		// send a job to a waiting job thread
		void DistributeJob(JobThread * waitingThread);

		// close the jobmanager and stop all worker threads
		bool Destroy();

	private:
		typedef LFQueue<Job*> JobQueue;
		typedef gtl::List<JobThread*> ThreadList;
		
		ThreadList m_idleThreads;
		ThreadList m_runningThreads;
		JobQueue m_jobQueue;
		ThreadEvent m_jobWaitEvent;
	};

}

#endif // #ifndef _jobqueue_h__